package com.hddf.project.auth.interceptor;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.kommii.log.LogWriter;

import com.hddf.project.auth.pojos.User;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;


	
	public class LogInterceptor extends AbstractInterceptor {  
		  
		private static final long serialVersionUID = 565445990123435L;

		@Override  
	    public  String intercept(ActionInvocation invocation) throws Exception { 
			String res = invocation.invoke();
	        // 取得请求相关的ActionContext实例  
	        HttpServletRequest request = null;
	        //HttpServletResponse response = null;
	        request = (HttpServletRequest) invocation.getInvocationContext().get(ServletActionContext.HTTP_REQUEST);
	       // response = (HttpServletResponse) invocation.getInvocationContext().get(ServletActionContext.HTTP_RESPONSE);
	        System.out.println("+++++ logs Interceptor +++++++");
	      
	        Map<String,Object> map = (HashMap<String,Object>)request.getAttribute("user");
	        if(null!=map){
	        	User u = (User)map.get("u");
		        String uname = "uname: "+u.getUsername()+" ,userid: "+u.getId();
				String uri = (String)request.getAttribute("uri");
				System.out.println(uri);
				uname = uname+" ,action :"+uri;
				System.out.println(uname);
				String logname = (String)request.getAttribute("logname");
				System.out.println(logname);
				String logs = (String)request.getAttribute("logs");
				System.out.println(logs);
				if(null!=logname){
					LogWriter.writeLog(logname,uname,logs);
				}
	        }
	        
			return res;
					
		}
	}


